/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidades.controladores;

import entidades.Caracteristicas;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author alphyon
 */
public class CaracteristicasJDBC {
    PreparedStatement ps = null;
    Connection conn = null;
    ResultSet rs = null;


    public CaracteristicasJDBC(){
        cargarDriver();
        coneccion();

    }

     public void cargarDriver(){
       try{
           Class.forName("org.gjt.mm.mysql.Driver");
           System.out.println("Carga de driver Correcta......");
       }catch(Exception e){
           System.out.print("A ocurrido Un problema" +
                   "en la caraga del Driver"+e.getMessage());
       }
   }

   public void coneccion(){

       try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventario_control", "chepe", "gundam");
            System.out.println("Conexion Satisfactoria...");
        } catch(SQLException e) {
            e.printStackTrace();
        }

   }


    public int crearCaracteristicas(Caracteristicas car) {
        int flag=0;

        try{
        String SQL="";
        SQL=SQL+"INSERT INTO caracteristicas (id,marca,modelo,serie,id_tipo,caracteristicas,id_equipo)";
        SQL=SQL+" VALUES(?,?,?,?,?,?,?);";

        ps = conn.prepareStatement(SQL);

        ps.setInt(1, car.getId());
        ps.setString(2, car.getMarca());
        ps.setString(3, car.getModelo());
        ps.setString(4, car.getSerie());
        ps.setString(5, car.getIdTipo());
        ps.setString(6, car.getCaracteristicas());
        ps.setString(7, car.getIdEquipo());


        flag=ps.executeUpdate();

        }catch(Exception e){
           e.printStackTrace();
        }finally{
             try {
              if (ps != null) ps.close();
              if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

       return flag;
    }

    public int borrarRegistroCaracteristicas(int id){
        int flag=0;
        String SQL = "DELETE FROM cariguraciones WHERE id=?";


        try {
            ps = conn.prepareStatement(SQL);
            ps.setInt(1, id);
            flag=ps.executeUpdate();
        } catch (SQLException e) {
           e.printStackTrace();
        }

        return flag;
    }
    public int actualizarCaracteristicas(Caracteristicas car) {
        int flag=0;

        try{
        String SQL="";
        SQL=SQL+"UPDATE caracteristicas  SET marca=?,modelo=?,serie=?,id_tipo=?,caracteristicas=?,id_equipo=? WHERE id=?";


        ps = conn.prepareStatement(SQL);


        ps.setInt(7, car.getId());
        ps.setString(1, car.getMarca());
        ps.setString(2, car.getModelo());
        ps.setString(3, car.getSerie());
        ps.setString(4, car.getIdTipo());
        ps.setString(5, car.getCaracteristicas());
        ps.setString(6, car.getIdTipo());

        flag=ps.executeUpdate();

        }catch(Exception e){
           e.printStackTrace();
        }finally{
             try {
              if (ps != null) ps.close();
              if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

       return flag;
    }
    public LinkedList<Caracteristicas> getCaracteristicas(){
       LinkedList<Caracteristicas> lista=new LinkedList<Caracteristicas>();

       String SQL="SELECT * FROM caracteristicas";
        try {
            ps = conn.prepareStatement(SQL);
            rs = ps.executeQuery();

            while(rs.next()){

                Caracteristicas car = new Caracteristicas();
                car.setId(rs.getInt("id"));
                car.setMarca(rs.getString("marca"));
                car.setModelo(rs.getString("modelo"));
                car.setSerie(rs.getString("serie"));
                car.setIdTipo(rs.getString("id_tipo"));
                car.setCaracteristicas(rs.getString("caracteristicas"));
                car.setIdEquipo(rs.getString("id_equipo"));





                lista.add(car);

            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
             try {
              if (rs != null) rs.close();
              if (ps != null) ps.close();
              if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return lista;


   }


}
